package com.joysw.ui.adapter;

import java.util.ArrayList;


import com.joysw.R;
import com.joysw.models.DtqModel;
import com.joysw.ui.FenXiangActivity;
import com.joysw.ui.FoodsDetailActivity;
import com.joysw.ui.ImagePagerActivity;
import com.joysw.view.NoScrollGridView;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @Title: DtqListAdapter.java
 * @Package com.joysw.ui.adapter
 * @Description: TODO(用一句话描述该文件做什么)
 * @author LIMINGYAN
 * @date 2015年3月29日 下午3:28:13
 * @version V1.0
 */
public class DtqListAdapter extends BaseAdapter {

	private ArrayList<DtqModel> mList;
	private LayoutInflater mInflater;
	private Context mContext;

	public DtqListAdapter(Context context, ArrayList<DtqModel> list) {
		mInflater = LayoutInflater.from(context);
		mContext = context;
		this.mList = list;
	}

	@Override
	public int getCount() {
		return mList == null ? 0 : mList.size();
	}

	@Override
	public DtqModel getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return getItem(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item_dtq, null);
			holder.avator = (ImageView) convertView
					.findViewById(R.id.avator_dtq);
			holder.name = (TextView) convertView.findViewById(R.id.name_dtq);
			holder.content = (TextView) convertView
					.findViewById(R.id.content_dtq);
			holder.gridView = (NoScrollGridView) convertView
					.findViewById(R.id.gridView_dtq);
			holder.fxBtn = (Button) convertView.findViewById(R.id.fx_btn_dtq);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final DtqModel bean = getItem(position);
		ImageLoader.getInstance().displayImage(bean.avator, holder.avator);
		holder.name.setText(bean.name);
		holder.content.setText(bean.content);
		holder.content.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 跳转到物品页面
				Intent intent = new Intent(mContext, FoodsDetailActivity.class);
				mContext.startActivity(intent);
			}
		});
		holder.fxBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// 跳转到分享页面
				Intent intent = new Intent(mContext, FenXiangActivity.class);
				mContext.startActivity(intent);
			}
		});

		if (bean.urls != null && bean.urls.length > 0) {
			holder.gridView.setVisibility(View.VISIBLE);
			holder.gridView.setAdapter(new MyGridAdapter(bean.urls, mContext));
			holder.gridView
					.setOnItemClickListener(new AdapterView.OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							imageBrower(position, bean.urls);
						}
					});
		} else {
			holder.gridView.setVisibility(View.GONE);
		}
		return convertView;
	}

	private void imageBrower(int position, String[] urls) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	private static class ViewHolder {
		public TextView name;
		public ImageView avator;
		TextView content;
		NoScrollGridView gridView;
		public Button fxBtn;// 分享
		public Button scBtn;// 收藏
	}
}
